//
//  NetManager.m
//  Net2
//
//  Created by Justsoso on 16/3/29.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "NetManager.h"
#import "def.h"

@implementation NetManager


+ (BOOL)longWithUserName:(NSString *)username andPassWord:(NSString *)password
{
    NSString *str = [NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@&method=get&type=JSON",username,password];
    NSURL *url = [NSURL URLWithString:str];
    
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    
    NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
    NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",result);
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    if (dict[@"error"] != nil)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}



+ (void)longWithUserName:(NSString *)username
             andPassWord:(NSString *)password
            successBlock:(void (^)())successBlock
             failedBlock:(void(^)(NSError *))failBlock
{
    NSString *str = [NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@&method=get&type=JSON",username,password];
    [self requestInfoWithURLStr:str successBlock:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        if (dict[@"error"] != nil)
        {
            NSError *error = [NSError errorWithDomain:@"com.niit.net2Demo" code:kErrorLogin userInfo:@{NSLocalizedDescriptionKey:dict[@"error"]}];
            failBlock(error);
        }
        else
        {
            successBlock();
        }
    } faileBlock:^(NSError *error) {
        failBlock(error);
    }];
    
}

+ (void)requestMovieListWithSuccessBlock:(void (^)(NSArray *arr))successBlock
                             failedBlock:(void (^)(NSError *error))failBlock
{
    /* 请求地址的字符串 */
    NSString *str = @"http://localhost:8080/MJServer/video?method=get&type=JSON";
    [self requestInfoWithURLStr:str successBlock:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        successBlock(dict[@"videos"]);
    } faileBlock:^(NSError *error) {
        failBlock(error);
    }];
}

+ (void)requestInfoWithURLStr:(NSString *)urlStr
                 successBlock:(void (^)(NSData *data))successBlock
                   faileBlock:(void (^)(NSError *error))faileBlock
{
    NSURL *url = [NSURL URLWithString:urlStr];
    
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError == nil)
        {
            successBlock(data);
        }
        else
        {
            faileBlock(connectionError);
        }
    }];
}
@end
